import pickle

class Character:
    def __init__(self):
        self.name = ""
        self.race = ""
        self.height = ""
        self.weight = ""

def ListCharacters(myChars):
    print "Character List:"
    print "---------------------------"
    for character in myChars:
        print character.name, "- Color:", character.race , "- height:", character.height, "- weight:", character.weight
    
    
def ShowMenu():
    print "Welcome to  Spencer's friend list. "
    print "Come in and look at my menu.:"
    print '      [1] List characters.'
    print '      [2] Create characters.'
    print '      [3] Delete characters.'
    print '      [4] Edit characters.'
    print '      [5] Leave!'
    
    return raw_input()

def LoadCharacters():
    charList = []
    try:
        pickleFile = open("characters.pkl", "r")
        charList = pickle.load(pickleFile)
    except IOError as e:
        charList = []
       
    return charList

def SaveCharacters(charList):
    try:
        pickleFile = open("characters.pkl", "w")
        pickle.dump(charList, pickleFile)
        print "Saved characters successfully!"
    except IOError as e:
        print "Failed to save characters!"
       
    return charList

def DeleteCharacter(charList):
    run = True
    numberOfChars = len(charList)
    while run :
        print "Choose a character to delete:"
        print "---------------------------"
        for character in charList:
            currentIndex = charList.index(character)+1
            print ("["+str(currentIndex)+"]"), character.name
        choice = int(raw_input())
        if (choice >= 1 and choice <= numberOfChars):
            characterFromList = charList[choice-1]
            print "Deleting", characterFromList.name, "...",
            del charList[choice-1]
            print "DONE!"
            run = False
        else:
            print "Invalid Choice!"


def EditCharacter(charList):
    run = True
    numberOfChars = len(charList)
    while run :
        print "Choose a character to edit:"
        print "---------------------------"
        for character in charList:
            currentIndex = charList.index(character)+1
            print ("["+str(currentIndex)+"]"), character.name
            
        choice = int(raw_input())
        if (choice >= 1 and choice <= numberOfChars):
            char = charList[choice-1]
            print "Editing "+ char.name+ "..."

            #------------------------------
            print "what is its name? (press enter to keep \""+char.name+"\")",
            newName = raw_input()
            if newName != "":
                char.name = newName
            #------------------------------
            editing = True    
            while editing:
                print "what race: (press enter to keep \""+char.race+"\")"
                print "    [1]white"
                print "    [2]black"
                newRace = raw_input()
                
                editing = False
                if newRace == "":
                    char.race = char.race
                elif newRace == "1":
                    char.race = "white" 
                elif newRace == "2": 
                    char.race = "black" 
                else:
                    editing = True
                    print "Invalid choice"
                    
            #------------------------------  
            editing = True    
            while editing:
                print "what height: (press enter to keep \""+char.height+"\")"  
                print "    [1]tall"
                print "    [2]average"
                print "    [3]small"        
                newHeight = raw_input() 
          
                editing = False
                if newHeight == "":
                    char.height = char.height
                elif newHeight == "1":
                    char.height = "tall" 
                elif newHeight == "2": 
                    char.height = "average"
                elif newHeight == "3":
                    char.height = "small"                    
                else:
                    editing = True
                    print "Invalid choice"                    
            #------------------------------ 
            editing = True    
            while editing:
                print "what weight: (press enter to keep \""+char.weight+"\")"  
                print "    [1]fat"
                print "    [2]average"
                print "    [3]thin"        
                newWeight = raw_input()      
                editing = False
                if newWeight == "":
                    char.weight = char.weight
                elif newWeight == "1":
                    char.weight = "fat" 
                elif newWeight == "2": 
                    char.weight = "average"
                elif newWeight == "3":
                    char.weight = "thin"                    
                else:
                    editing = True
                    print "Invalid choice"             
                    
            print "DONE!"
            run = False
        else:
            print "Invalid Choice!"
            
def CreateCharacter():
    Char = Character()
    print "what is its name?",
    Char.name = raw_input()
    
    GetRace = True
    while GetRace:
        print "what race:"
        print "    [1]white"
        print "    [2]black"
        Char.race = raw_input()
        if Char.race == "1":
            Char.race = "white"
            GetRace = False
        elif Char.race == "2":
            Char.race = "black"
            GetRace = False            
    GetHeight = True
    while GetHeight:
        print "what height:"
        print "    [1]tall"
        print "    [2]average"
        print "    [3]small"        
        Char.height = raw_input()
        if Char.height == "1":
            Char.height = "tall"
            GetHeight = False
        elif Char.height == "2":
            Char.height = "average"
            GetHeight = False         
        elif Char.height == "3":
            Char.height = "small"
            GetHeight = False
    GetWeight = True
    while GetWeight:
        print "what weight:"
        print "    [1]fat"
        print "    [2]average"
        print "    [3]thin"        
        Char.weight = raw_input()
        if Char.weight == "1":
            Char.weight = "fat"
            GetWeight = False
        elif Char.weight == "2":
            Char.weight = "average"
            GetWeight = False         
        elif Char.weight == "3":
            Char.weight = "thin"
            GetWeight = False
            
    return Char
            
def InvalidChoice(choice):
    print "Choice",choice,"is invalid you #$&$$%*! "
    print '_______________________________________________'
    
myChars = LoadCharacters()
    
running = True
while running:    
    choice = ShowMenu()
    if choice == "1":
        ListCharacters(myChars)
    elif choice == "2":
        myChars.append(CreateCharacter())
        SaveCharacters(myChars)
    elif choice == "3":
        DeleteCharacter(myChars)
        SaveCharacters(myChars)
    elif choice == "4":
        EditCharacter(myChars)
        SaveCharacters(myChars)
    elif choice == "5":
        running = False
        print "ALERT!!! I need a new motherboard,CPU and power supply"
        print "Goodbye !@#$@ and never come back you @!@#$^%#@# !!!"
    else:
        InvalidChoice(choice)
